﻿/*globals _comma_separated_list_of_variables_*/
//## LEAGAL
/*
#region - Legal -

Copyright © 2011, Tateeda Media Network
All rights reserved. http://tateeda.com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Neither the name of the Tateeda Media Network, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

#endregion - Legal -

*/
//#end
$(document).ready(function () {
    init();
});

function init() {
    cleanInput();
    $.ajax({
        type: "POST",
        url: "/Medication/GetPrescriptionDrugCategory",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            LoadPrescriptionsCategory(data);
        },
        error: function (e) {
            alert("error " + e.responseText);
        }
    });

    $("#drugName").dblclick(function () {
        EditDrugName($("#drugName").val(), $("#drugName option:selected").text());
    });
}

function cleanInput() {
    $('#drugCategory').find('option').remove().end();
    $('#newDrugCategory').find('option').remove().end();
    $('#newClassCategory').find('option').remove().end();
    $('#drugClass').find('option').remove().end();
    $('#drugName').find('option').remove().end();
}

function LoadPrescriptionsCategory(data) {
    $('#newDrugCategory').append('<option>--Select--</option>');
    $('#newClassCategory').append('<option>--Select--</option>');

    if (data === undefined || data == null) return;

    $.each(data, function (id, obj) {
        if (obj.Name != "Over Counter Drugs") {
            $('#drugCategory').
                append($("<option></option>").
                        attr("value", obj.DrugCategoryId).
                        text(obj.Name));

            $('#newDrugCategory').
                append($("<option></option>").
                        attr("value", obj.DrugCategoryId).
                        text(obj.Name));

            $('#newClassCategory').
                append($("<option></option>").
                        attr("value", obj.DrugCategoryId).
                        text(obj.Name));
        }
    });

    $('#drugCategory').change(function () {
        GetPrescriptionClass($("#drugCategory option:selected").val());
    });

    $('#newDrugCategory').change(function () {
        GetNewClassSelection($("#newDrugCategory option:selected").val());
    });
}

function GetPrescriptionClass(id) {
    if (id > 0) {
        $.ajax({
            type: "POST",
            url: "/Medication/GetPrescriptionDrugClass",
            data: "{'categoryId':'+" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                LoadPrescriptionClass(data);
            },
            error: function (e) {
                alert("error " + e.responseText);
            }
        });
    }
}

function GetNewClassSelection(id) {
    if (id > 0) {
        $.ajax({
            type: "POST",
            url: "/Medication/GetPrescriptionDrugClass",
            data: "{'categoryId':'+" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                LoadNewClass(data);
            },
            error: function (e) {
                alert("error " + e.responseText);
            }
        });
    }
}

function LoadNewClass(data) {
    $('#newDrugClass')
        .find('option')
        .remove()
        .end()
        .append('<option>--Select--</option>');

    if (data === undefined || data == null) return;

    $.each(data, function (id, obj) {
        $('#newDrugClass').
            append($("<option></option>").
                    attr("value", obj.DrugClassId).
                    text(obj.Name));
    });
}

function LoadPrescriptionClass(data) {
    $('#drugClass')
        .find('option')
        .remove()
        .end();

    $('#drugName')
        .find('option')
        .remove()
        .end();

    if (data === undefined || data == null) return;

    $.each(data, function (id, obj) {
        $('#drugClass').
            append($("<option></option>").
                    attr("value", obj.DrugClassId).
                    text(obj.Name));
    });

    $('#drugClass').change(function () {
        GetPrescriptionName($("#drugClass option:selected").val());
    });
}

function GetPrescriptionName(id) {
    $('#drugName')
        .find('option')
        .remove()
        .end();

    if (id != 7) {
        $('#drugName').show();
        $("#divDrugName").html('');
        $("#divEditDrugName").html('');
        $.ajax({
            type: "POST",
            url: "/Medication/GetPrescriptionDrugName",
            data: "{'classId':'" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                LoadPrescriptionName(data);
            },
            error: function (e) {
                alert("error " + e.responseText);
            }
        });
    } else {
        $('#drugName').hide();
        GetPrescriptionNamePaged(id, 0);
    }
}

function GetPrescriptionNamePaged(classId, pageIndex) {
    $("#DrugNameToEdit").val('');
    $("#hidDrugIdToEdit").val('');
    if (pageIndex < 1) pageIndex = 0;
    $("#divEditDrugName").html('');
    $.ajax({
        type: "POST",
        url: "/Medication/GetPrescriptionDrugNamePaged",
        data: "{'classId':'" + classId + "', 'pageIndex':'" + pageIndex + "','pageSize':'20'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            LoadPrescriptionNamePaged(data, pageIndex);
        },
        error: function (e) {
            alert("error " + e.responseText);
        }
    });
}

function LoadPrescriptionNamePaged(data, pageIndex) {
    if (pageIndex > data.total) pageIndex = data.total;

    var html = "";
    for (var i = 0; i < data.drugs.length; i++) {
        html += "<a onclick=\"EditDrugName('" + data.drugs[i].DrugId + "','" + data.drugs[i].Name + "');\">" + data.drugs[i].Name + "</a><br/>";
    }
    html += "<hr/>";
    html += "Goto Page: <input type='text' style='width:30px;' id='pageIndex'> ";
    html += " <input type='button' class='ButtonGlobalShort' style='width:40px;' value='Go' onclick=\"GetPrescriptionNamePaged('7', $('#pageIndex').val());\"/> Page: " + pageIndex + " of " + (data.total - 1);
    html += " <a href='#' onclick=\"GetPrescriptionNamePaged('7', " + (pageIndex - 1) + ");\">Back</a>";
    html += " <a href='#' onclick=\"GetPrescriptionNamePaged('7'," + (pageIndex + 1) + ");\">Next</a>";

    html += "<br/>";
    $("#divDrugName").css("text-align", "left").css("cursor", "defautl").html(html);
}

function EditDrugName(id, name) {
    var html = "<hr/>";
    html += "<input type='hidden' value='" + id + "' id='hidDrugIdToEdit'/>";
    html += "<input type='text' value='" + name + "' id='DrugNameToEdit' style='width:100%; background-color:yellow;'>";
    html += " <input type='button' class='buttonGlobalShort' value='Update' onclick='UpdateDrugName();'>";
    $("#divEditDrugName").html(html);
}

function UpdateDrugName() {
    var newName = $("#DrugNameToEdit").val();
    var drugId = $("#hidDrugIdToEdit").val();
    $.ajax({
        type: "POST",
        url: "/Medication/UpdateDrugName",
        data: "{'drugId':'" + drugId + "', 'name':'" + newName + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            alert("Drug is updated");
        },
        error: function (e) {
            alert("error " + e.responseText);
        }
    });

    $("#DrugNameToEdit").val('');
    $("#hidDrugIdToEdit").val('');
}

function LoadPrescriptionName(data) {
    $('#drugName')
        .find('option')
        .remove()
        .end();

    if (data === undefined || data == null) return;

    $.each(data.drugs, function (id, obj) {
        var text = obj.Name;
        var value = obj.DrugId;
        $('#drugName').
            append($("<option></option>").val(value).html(text));
    });
}

function AddNewDrug() {
    //AddNewPrescriptionDrug(string name, int classId)
    var name = $("#newDrugName").val();
    var classId = $("#newDrugClass").val();
    var drugTypeId = 1;
    if (classId == 8) {
        drugTypeId = 2;
    }

    $.ajax({
        type: "POST",
        url: "/Medication/AddNewPrescriptionDrug",
        data: "{'name':'" + name + "','classId':'" + classId + "','drugTypeId':'" + drugTypeId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            $("#newDrugName").val('');
            $("#newDrugClass").val('');
            init();
        },
        error: function (e) {
            alert("error " + e.responseText);
        }
    });
}

function AddNewClass() {
    //AddNewPrescriptionClass(string name, int categoryId)
    var name = $("#newClassName").val();
    var categoryId = $("#newClassCategory").val();
    $.ajax({
        type: "POST",
        url: "/Medication/AddNewPrescriptionClass",
        data: "{'name':'" + name + "','categoryId':'" + categoryId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            $("#newClassName").val('');
            init();
        },
        error: function (e) {
            alert("error " + e.responseText);
        }
    });
}

function AddNewCategory() {
    var name = $("#newCategory").val();

    $.ajax({
        type: "POST",
        url: "/Medication/AddNewPrescriptionCategory",
        data: "{'name':'" + name + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            $("#newCategory").val('');
            init();
        },
        error: function (e) {
            alert("error " + e.responseText);
        }
    });
}

function DeleteCategory() {
    var categoryId = $("#drugCategory").val();
    if (!categoryId) return;
    var okToDelete = confirm('Are you sure you want to Delete?');
    if (okToDelete) {
        $.ajax({
            type: "POST",
            url: "/Medication/DeletePrescriptionCategory",
            data: "{'categoryId':'" + categoryId + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                init();
            },
            error: function (e) {
                alert("error " + e.responseText);
            }
        });
    }
}

function DeleteClass() {
    var classId = $("#drugClass").val();
    if (!classId) return;

    var okToDelete = confirm('Are you sure you want to Delete?');
    if (okToDelete) {
        $.ajax({
            type: "POST",
            url: "/Medication/DeletePrescriptionClass",
            data: "{'classId':'" + classId + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                init();
            },
            error: function (e) {
                alert("error " + e.responseText);
            }
        });
    }
}

function DeleteDrug() {
    var drugId = $("#drugName").val();
    if (!drugId) return;

    var okToDelete = confirm('Are you sure you want to Delete?');
    if (okToDelete) {
        if (!drugId) {
            drugId = $("#hidDrugIdToEdit").val();
        }
        if (drugId > 0) {
            $.ajax({
                type: "POST",
                url: "/Medication/DeletePrescriptionDrug",
                data: "{'drugId':'" + drugId + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    init();
                    $("#divDrugName").html('');
                    $("#divEditDrugName").html('');
                    alert('Drug has been deleted');
                },
                error: function (e) {
                    alert("error " + e.responseText);
                }
            });
        }
    }
}